package com.module.user.ui.ringinfo

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyEvents
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.*
import com.common.base.listener.AlertDialogConfirmBtnListener
import com.common.base.utils.StatusBarUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.module.ble.listener.CusConnectBleDeviceExpandListener
import com.module.user.R
import com.module.ble.utils.AizoSDKUtil
import com.module.ble.utils.AizoSDKUtil.onDestroyRingSdk
import com.module.ble.utils.AizoSDKUtil.removeBleDeviceInfoInApp
import com.module.ble.utils.BluetoothPairingUtil.unpairBluetoothDevice
import com.module.user.databinding.UserActivityRingInfoBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/03/20
 * <p>
 * 描述：戒指信息页面
 * <p>
 * 修订历史：
 */
@Route(path = RouterHub.ROUTER_RING_INFO_ACTIVITY)
class RingInfoActivity : AppActivity<UserActivityRingInfoBinding, RingInfoViewModel>(), View.OnClickListener,
    CusConnectBleDeviceExpandListener {

    override fun getViewModel() = RingInfoViewModel::class.java
    override fun getViewContentBinding() = UserActivityRingInfoBinding.inflate(layoutInflater)
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)
        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(resources.getString(R.string.ac_ring_info_title))
        headTitleUtils.setLineStyle(10)
    }

    override fun initEvents() {
        super.initEvents()
        AizoSDKUtil.addConnectBleDeviceExpandListener(this)
        mViewContentBinding.kvRestoreFactorySetting.setOnClickListener(onClick = this)
    }

    override fun lazyInitData() {
        super.lazyInitData()
        mViewContentBinding.kvFirmwareVersion.setValue("V${AizoSDKUtil.getBindRingInfo()?.version}")
        mViewContentBinding.kvMacAdder.setValue(AizoSDKUtil.getBindRingInfo()?.getRingMacStr())
    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.kvRestoreFactorySetting -> {
                showAlertDialogTwoBtn(
                    getString(R.string.cancel_btn_zh),
                    getString(R.string.ok_btn_zh2),
                    getString(R.string.common_hint_title),
                    getString(R.string.common_ring_restore_factory_setting_hint),
                    message2 ="",
                    true,
                    object : AlertDialogConfirmBtnListener {
                        override fun onCancelBtnClick() {
                        }

                        override fun onConfirmBtnClick() {
                            showDialogLoading(false)
                            unpairBluetoothDevice(AizoSDKUtil.curMac?:"").run {
                                AizoSDKUtil.handleDevice(2)
                                postDelay(3000) {//  延时3秒
                                    removeDeviceInfo()
                                    LiveEventBus.get(KeyEvents.KEY_MAIN_SWITCH_TAB).post(0)
                                }
                            }
                        }
                    }
                )
            }
        }
    }

    override fun onDestroy() {
        AizoSDKUtil.removeConnectBleDeviceExpandListener(this)
        super.onDestroy()
    }

    override fun onConnect(macStr: String?) {
    }

    override fun onConnectError(macStr: String?, throwable: Throwable?, state: Int) {
    }

    override fun onDisconnect(macStr: String?) {
    }

    override fun onHandleDevice(type: Int, result: Boolean) {
    }

    override fun onBoundDeviceResult(macStr: String, result: Boolean) {
    }

    private fun removeDeviceInfo() {
        removeBleDeviceInfoInApp()
        onDestroyRingSdk()
        mViewContentBinding.kvFirmwareVersion.setValue("V ---")
        mViewContentBinding.kvMacAdder.setValue("--")
        dismissLoadingDialog()
    }

    private fun requestVersion() {
        showDialogLoading()
        mViewModel.requestVersion().observe(this) {
            dismissLoadingDialog()
            it.isSuccess().yes {
                it.Data?.let { it1 ->
                }
            }.otherwise {
            }
        }
    }
}